﻿/*------------------------------------------------------------------
-- COPYRIGHT (C) 2010-2012  Atom
-- ALL RIGHTS RESERVED.
-- 兆尹科技
-- CREATE DATE: 2010/07/12
-- CREATE MAN:liujian	
-- 集合，元素唯一类
-- MODIFY HISTORY:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
-- MODIFY DATE:
-- MODIFY MAN:	
-- MODIFY DESC:
---------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;

namespace Atom.Common
{
    /// <summary>
    /// 集合，元素唯一
    /// </summary>
    /// <typeparam name="T">元素类型</typeparam>
    public class UniqueList<T> : IEnumerable<T>
    {
        #region 初始化
        private List<T> list;
        private Comparison<T> comparison;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="comparison">比较相同的条件，如果为true，则添加无效。</param>
        public UniqueList(Comparison<T> comparison)
        {
            list = new List<T>();
            this.comparison = comparison;
        } 
        #endregion

        #region 公共方法
        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="t">元素</param>
        /// <returns>true：元素已添加；false元素未添加</returns>
        public bool Append(T t)
        {
            bool isAppend = true;
            foreach (var item in list)
            {
                if (comparison(item, t) == 0)
                {
                    isAppend = false;
                    break;
                }
            }
            if (isAppend)
            {
                list.Add(t);
            }
            return isAppend;
        }

        /// <summary>
        /// 添加元素
        /// </summary>
        /// <param name="list"></param>
        public void AppendRange(List<T> list)
        {
            foreach (var item in list)
            {
                this.Append(item);
            }
        }

        /// <summary>
        /// 可读列表
        /// </summary>
        public ReadOnlyCollection<T> List
        {
            get
            {
                return this.list.AsReadOnly();
            }
        }

        /// <summary>
        /// 列表大小
        /// </summary>
        public int Count
        {
            get
            {
                return this.list.Count;
            }
        }

        /// <summary>
        /// 索引
        /// </summary>
        /// <param name="index">根据索引获取元素</param>
        /// <returns></returns>
        public T this[int index]
        {
            get
            {
                return this.list[index];
            }
            set
            {
                this.list[index] = value;
            }
        }

        /// <summary>
        /// 根据索引清除元素
        /// </summary>
        /// <param name="index">索引号</param>
        public void RemoveAt(int index)
        {
            this.list.RemoveAt(index);
        }

        /// <summary>
        /// 清除集合
        /// </summary>
        public void Clear()
        {
            this.list.Clear();
        } 
        #endregion

        #region IEnumerable<T> 成员
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public IEnumerator<T> GetEnumerator()
        {
            return this.list.GetEnumerator();
        }        

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.list.GetEnumerator();
        }

        #endregion
    }
}
